#ifndef MVDMAINWINDOW_H
#define MVDMAINWINDOW_H

#include <QMainWindow>
#include <QStringListModel>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QItemDelegate>
#include <QComboBox>
#include <QDateEdit>

class CustomDelegate : public QItemDelegate
{
public:
    CustomDelegate(QObject *parent = Q_NULLPTR) : QItemDelegate(parent){}

    //构造代理组件，QComboBox
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        QComboBox *genderBox = new QComboBox(parent);
        return genderBox;
    }

    void setEditorData(QWidget *editor, const QModelIndex &index) const override
    {
        QComboBox *genderBox  = static_cast<QComboBox*>(editor);
        genderBox->addItems(QStringList()<<"男"<<"女");
    }

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        editor->setGeometry(option.rect);
    }

    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
    {
        QComboBox *genderBox  = static_cast<QComboBox*>(editor);
        QString gender = genderBox->currentText();

        model->setData(index,QVariant(gender));
    }
};

class BirthDelegate : public QItemDelegate
{
public:
    BirthDelegate(QObject *parent = Q_NULLPTR) : QItemDelegate(parent){}

    //构造代理组件，QComboBox
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        QDateEdit *genderBox = new QDateEdit(parent);
        genderBox->setCalendarPopup(true);
        return genderBox;
    }

    void setEditorData(QWidget *editor, const QModelIndex &index) const override
    {
        QDateEdit *genderBox  = static_cast<QDateEdit*>(editor);
        genderBox->setDate(QDate::currentDate());
    }

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        editor->setGeometry(option.rect);
    }

    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
    {
        QDateEdit *genderBox  = static_cast<QDateEdit*>(editor);
        QString gender = genderBox->date().toString("yyyy-MM-dd");

        model->setData(index,QVariant(gender));
    }
};

namespace Ui {
class MVDMainWindow;
}

class MVDMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MVDMainWindow(QWidget *parent = 0);
    ~MVDMainWindow();

    QSqlTableModel * t_model;
    void initSqlTableMode();
    void testMVDview();

private slots:
    void on_commitAction_triggered();

    void on_revertAction_triggered();

    void on_addAction_triggered();

    void on_removeAction_triggered();

    void on_selectAction_triggered();

    void on_refreshAction_triggered();

private:
    Ui::MVDMainWindow *ui;
};

#endif // MVDMAINWINDOW_H
